package day04;

import day04.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;

public class Demo05Money2 {
    public static void main(String[] args)throws Exception {
        //1.创建QueryRunner对象
        QueryRunner qr = new QueryRunner();
        //2.获取连接
        Connection conn = DruidUtils.getConnection();
        try {
            conn.setAutoCommit(false);

            //3.准备sql
            String outMoney = "update account set money = money-? where name = ?";
            String inMoney = "update account set money = money+? where name = ?";
            //4.执行sql
            qr.update(conn, outMoney, 1000, "taoge");

             //System.out.println(1/0);

            qr.update(conn, inMoney, 1000, "yixing");

            conn.commit();
            System.out.println("转账成功");

        } catch (Exception e) {
            conn.rollback();
            System.out.println("出现异常，回滚");
            e.printStackTrace();
        }finally {
            DruidUtils.close(conn, null, null);
        }
    }

}
